package com.tianma315.admin.agent.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.agent.dao.AgentAddressDao;
import com.tianma315.admin.agent.dao.AgentDao;
import com.tianma315.admin.agent.domain.AgentAddressDO;
import com.tianma315.admin.agent.domain.AgentDO;
import com.tianma315.admin.agent.service.AgentAddressService;
import com.tianma315.admin.agent.service.AgentService;
import com.tianma315.admin.agent.vo.AgentVO;
import com.tianma315.admin.common.base.CoreServiceImpl;
import com.tianma315.admin.common.exception.MessageException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * <pre>
 * 经销商
 * </pre>
 * <small> 2019-05-21 15:57:56 | zcm</small>
 */
@Service
public class AgentServiceImpl extends CoreServiceImpl<AgentDao, AgentDO> implements AgentService {

    @Autowired
    private AgentAddressService agentAddressService;


    @Override
    public Page<AgentDO> getPage(int pageNum, int pageSize, String searchKey) {
        Page<AgentDO> page = new Page<>(pageNum, pageSize);


        selectPage(page, new EntityWrapper<AgentDO>() {{
            like("agent_name", searchKey);
            orderBy("create_date",false);
        }});

        return page;
    }

    @Override
    public Page<AgentDO> getEnablePage(int pageNum, int pageSize, String searchKey) {
        Page<AgentDO> page = new Page<>(pageNum, pageSize);
        return selectPage(page, new EntityWrapper<AgentDO>() {{
            ne("status", AgentDO.STATUS_DISABLE);
            like("agent_name", searchKey);
        }});
    }

    @Override
    public boolean save(AgentVO agent) {
        List<AgentDO> list = selectList(new EntityWrapper<AgentDO>() {{
            eq("agent_name", agent.getAgentName());
        }});

        if (list == null || list.isEmpty()){
            agent.setCreateDate(new Date());
            agent.setStatus(AgentDO.STATUS_ENABLE);
        }else{
            throw new MessageException(String.format("系统已经存在 %s", agent.getAgentName()));
        }

        insert(agent);

        //插入经销商地址
        AgentAddressDO agentAddressDO = new AgentAddressDO();
        BeanUtils.copyProperties(agent,agentAddressDO);
        return agentAddressService.addAgentAddress(agentAddressDO);
    }

    @Override
    public boolean edit(AgentVO agent) {
        AgentDO agentDO = selectById(agent.getAgentId());
        if (agent == null)
            throw new MessageException("您要修改的记录不存在");

        agentDO.setAgentName(agent.getAgentName());
        agentDO.setLinkman(agent.getLinkman());
        agentDO.setTel(agent.getTel());
        agentDO.setAddress(agent.getAddress());


        AgentAddressDO agentAddressDO = new AgentAddressDO();
        BeanUtils.copyProperties(agent,agentAddressDO);

        agentAddressService.updateById(agentAddressDO);

        return updateById(agentDO);
    }

    @Override
    public boolean changeStatus(long id, int status) {
        AgentDO agent = selectById(id);
        agent.setStatus(status);
        return updateById(agent);
    }

    @Override
    public AgentDO getById(Long agentId) {
        return selectById(agentId);
    }

    @Override
    public List<AgentDO> getAgentList() {
        Wrapper<AgentDO> wrapper = new EntityWrapper<>();
        wrapper.eq("status",1);
        List<AgentDO> list =selectList(wrapper);
        return list;
    }
}
